package subject.tengxun.middle;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * 功能：
 * 作者：yedou
 * 日期：2025/3/22 21:34
 */
public class merge {

    public static void main(String[] args) {
        int[][] intervals = {{1, 3}, {2, 6}, {8, 10}, {15, 18}};

        if (intervals.length == 0) {
//            return new int[0][2];
            System.out.println("no");
        }
        Arrays.sort(intervals, new Comparator<int[]>() {
            public int compare(int[] interval1, int[] interval2) {
                return interval1[0] - interval2[0];
            }
        });
        List<int[]> merged = new ArrayList<int[]>();
        for (int i = 0; i < intervals.length; ++i) {
            int L = intervals[i][0], R = intervals[i][1];
            if (merged.size() == 0 || merged.get(merged.size() - 1)[1] < L) {
                // 直接添加
                merged.add(new int[]{L, R});
            } else {
                merged.get(merged.size() - 1)[1] = Math.max(merged.get(merged.size() - 1)[1], R);
            }
        }
//        return merged.toArray(new int[merged.size()][]);
        System.out.println(merged);

        for (int[] interval : merged) {
            System.out.println(Arrays.toString(interval));
        }
    }
}